package com.ruoyi.tb.lmt.controller;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;
import java.util.Arrays;
import java.util.concurrent.TimeUnit;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.tb.lmt.domain.vo.TrackLengthVo;
import lombok.RequiredArgsConstructor;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.core.validate.QueryGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.tb.lmt.domain.vo.TbTrackInformationVo;
import com.ruoyi.tb.lmt.domain.bo.TbTrackInformationBo;
import com.ruoyi.tb.lmt.service.ITbTrackInformationService;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 基地轨道信息
 *
 * @author ruoyi
 * @date 2024-01-23
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/xha-lmt/trackInformation")
public class TbTrackInformationController extends BaseController {

    private final ITbTrackInformationService iTbTrackInformationService;

    /**
     * 查询基地轨道信息列表
     */
    //@SaCheckPermission("xha-lmt:trackInformation:list")
    @GetMapping("/list")
    public TableDataInfo<TbTrackInformationVo> list(TbTrackInformationBo bo, PageQuery pageQuery) {
        return iTbTrackInformationService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出基地轨道信息列表
     */
    //@SaCheckPermission("xha-lmt:trackInformation:export")
    @Log(title = "基地轨道信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(TbTrackInformationBo bo, HttpServletResponse response) {
        List<TbTrackInformationVo> list = iTbTrackInformationService.queryList(bo);
        ExcelUtil.exportExcel(list, "基地轨道信息", TbTrackInformationVo.class, response);
    }

    /**
     * 获取基地轨道信息详细信息
     *
     * @param id 主键
     */
    //@SaCheckPermission("xha-lmt:trackInformation:query")
    @GetMapping("/{id}")
    public R<TbTrackInformationVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(iTbTrackInformationService.queryById(id));
    }

    /**
     * 新增基地轨道信息
     */
    //@SaCheckPermission("xha-lmt:trackInformation:add")
    @Log(title = "基地轨道信息", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody TbTrackInformationBo bo) {
        return toAjax(iTbTrackInformationService.insertByBo(bo));
    }

    /**
     * 修改基地轨道信息
     */
    //@SaCheckPermission("xha-lmt:trackInformation:edit")
    @Log(title = "基地轨道信息", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody TbTrackInformationBo bo) {
        return toAjax(iTbTrackInformationService.updateByBo(bo));
    }

    /**
     * 删除基地轨道信息
     *
     * @param ids 主键串
     */
    //@SaCheckPermission("xha-lmt:trackInformation:remove")
    @Log(title = "基地轨道信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(iTbTrackInformationService.deleteWithValidByIds(Arrays.asList(ids), true));
    }

    //获取轨道剩余长度信息
    //@SaCheckPermission("xha-lmt:trackInformation:getTrackLength")
    @GetMapping("/getTrackLength")
    public R<?> getTrackLength() {
        List<TrackLengthVo> trackLength = iTbTrackInformationService.getTrackLength();
        for (TrackLengthVo vo : trackLength) {
            if (StringUtils.isNotBlank(vo.getRemainingLength())){
                Double length = Double.parseDouble(vo.getRemainingLength());
                vo.setRemainingLength(round(length,2)+"");
            }
        }
        return R.ok(trackLength);
    }
    public static double round(double value, int places) {
        if (places < 0) throw new IllegalArgumentException();

        BigDecimal bd = BigDecimal.valueOf(value);
        bd = bd.setScale(places, RoundingMode.HALF_UP);
        return bd.doubleValue();
    }
}
